<template>
  <div :class="[active ? 'active' : '']">
    {{ person.name }}--{{ info.age }} -- {{ info.double }}
  </div>
  <div>useStorage -{{ JSON.parse(storage).name }} -test-</div>
  <el-button type="success" plain @click="add">添加test</el-button>
  <el-button type="danger" @click="reset">重置</el-button>
  <el-input v-focus v-model="test"></el-input>
  <el-switch v-model="isDark" @change="toggleDark"></el-switch>
  <el-color-picker v-model="color" @change="changeTheme('')" />
  {{ useTime }}
  <el-collapse-transition>
    <div v-show="ifShow">
      <img src="@/assets/test.jpg" width="150" />
      <img src="/black.jpg" width="150" />
      <div class="transition-box">el-collapse-transition</div>
      <div class="transition-box">el-collapse-transition</div>
      <el-icon color="#f00" size="24"><Delete /></el-icon>
      <el-button @click="brotherEmit">兄弟组件传参</el-button>
    </div>
  </el-collapse-transition>
</template>
<script lang="ts" setup>
import bus from "@/utils/bus.js";
import { watch, reactive, watchEffect, ref, onBeforeUnmount } from "vue";
import { useRouter } from "vue-router";
import { Delete } from "@element-plus/icons-vue";
import { userInfo } from "../../store";
import { useDark, useToggle, useTimeAgo, useStorage } from "@vueuse/core";
import { ElMessage } from "element-plus/es/components";

const storage = useStorage('storage', '')
const useTime = useTimeAgo(new Date(2021, 6, 30));
const isDark = useDark();
const toggleDark = () => useToggle(isDark);
const color = ref("#f00");
const changeTheme = (color:string) => {
  document.documentElement.style.setProperty("--el-color-primary", color);
};
const test = ref("test");
const ifShow = ref(true);
const info = userInfo();
const router = useRouter();
const person = reactive({
  name: import.meta.env.VITE_BASE_URL,
  age: 28,
});
const active = true;
const arr = reactive([0, 1]);
const add = () => {
  ElMessage({ type: "success", message: "测试成功" });
  person.age++;
  arr[2] = 2;
};
const reset = () => {
  info.$reset();
  ifShow.value = !ifShow.value;
};
watch(
  () => person.age,
  (newVal, oldVal) => {
    console.log(newVal, oldVal);
  }
);
watch(arr, (newVal) => {
  console.log(newVal);
});
watchEffect(() => {
  if (person.age > 30) {
    console.log(person.name);
  }
});
const brotherEmit = () => {
  router.push("/sidebar");
};
onBeforeUnmount(() => {
  bus.emit("msg", "信息");
});
</script>
<style lang="scss" scoped>
.active {
  color: #f00;
  font-size: 32px;
  width: 300px;
  height: 300px;
  border:1px solid #f00;
}
</style>
